package com.ruoyi.basebill.service.impl.billIssue;

import com.ruoyi.basebill.domain.*;
import com.ruoyi.basebill.mapper.TWfzRemunerationMapper;
import com.ruoyi.basebill.service.billIssue.IGjjftService;
import com.ruoyi.basebill.service.billIssue.IGjjjrService;
import com.ruoyi.basebill.service.impl.WfzGjjftServiceImpl;
import com.ruoyi.basebill.service.impl.WfzGjjjrServiceImpl;
import com.ruoyi.basebill.service.impl.billIssue.util.BillIssueUtil;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

@Service
public class GjjftService implements IGjjftService {


    @Autowired
    private TWfzRemunerationMapper tWfzRemunerationMapper;

    @Autowired
    private BillIssueUtil billIssueUtil;

    @Autowired
    private WfzGjjftServiceImpl wfzGjjftService;

    @Override
    public int billIssue(Long billId) {
        WfzGjjft wfzGjjft = new WfzGjjft();
        //首先根据传入的单据id，查找对应的数据
        TWfzRemuneration tWfzRemuneration = tWfzRemunerationMapper.selectTWfzRemunerationByFid(billId);
        //获取薪酬总表的单据头信息
        String fcompany = tWfzRemuneration.getFcompany();//组织
        String fperiod = tWfzRemuneration.getFperiod();//薪酬期间
        //获取当前登录的用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String username = loginUser.getUsername();

        ArrayList<WfzGjjftentry> wfzGjjftentries = new ArrayList<>();

        //开始遍历薪酬总表的分录信息
        List<TWfzRemunerationentry> tWfzRemunerationentryList = tWfzRemuneration.getTWfzRemunerationentryList();
        if (tWfzRemunerationentryList != null && tWfzRemunerationentryList.size() >0){
            for (int i = 0; i < tWfzRemunerationentryList.size(); i++) {
                TWfzRemunerationentry tWfzRemunerationentry = tWfzRemunerationentryList.get(i);
                String fcostnumber = tWfzRemunerationentry.getFcostnumber();//成本中心
                String fcostname = tWfzRemunerationentry.getFcostname();//成本中心名称
                String fcostcompany = tWfzRemunerationentry.getFcostcompany();//成本中心所属财务组织
                BigDecimal fgjj = tWfzRemunerationentry.getFgjj();//公积金
                BigDecimal fgrgjj = tWfzRemunerationentry.getFgrgjj();//个人公积金

                //获取分录的结算方式
                String fjsfs = billIssueUtil.Jsfs(fcompany, fcostnumber);
                //成本中心属性：
                String costType = billIssueUtil.costType(fcostnumber);

                Long fissbdy = tWfzRemunerationentry.getFissbdy();//是否当月发放
                Long fislz = tWfzRemunerationentry.getFislz();//是否离职
                String fvourtype = tWfzRemunerationentry.getFvourtype();//凭证字

                WfzGjjftentry wfzGjjftentry = new WfzGjjftentry();

                wfzGjjftentry.setFcostnumber(fcostnumber);
                wfzGjjftentry.setFcostname(fcostname);
                wfzGjjftentry.setFcostcompany(fcostcompany);
                wfzGjjftentry.setFgjj(fgjj);
                wfzGjjftentry.setFgrgjj(fgrgjj);
                wfzGjjftentry.setFjsfs(fjsfs);
                wfzGjjftentry.setFcbzxxz(costType);
                wfzGjjftentry.setFisdy(fissbdy);
                wfzGjjftentry.setFislz(fislz);
                wfzGjjftentry.setFvourtype(fvourtype);

                wfzGjjftentries.add(wfzGjjftentry);//新增到单据分录
            }
        }
        //总人数就是薪酬总表里所有的人  合并纳税成本中心就是属性是同组织下合并纳税成本中心的人数 关联方就是非同组织和同组织下二级组织属性的
        //总人数
        BigDecimal zrs = BigDecimal.valueOf(wfzGjjftentries.size());
        //合并纳税成本中心人数
        int nscbzxrs = 0;
        //关联方人数
        int glf = 0;
        for (int i = 0; i < wfzGjjftentries.size(); i++) {
            WfzGjjftentry wfzGjjftentry = wfzGjjftentries.get(i);
            String fjsfs = wfzGjjftentry.getFjsfs();
            if ("同组织下合并纳税成本中心".equals(fjsfs)){
                nscbzxrs++;
            }
            if ("同组织下是二级组织".equals(fjsfs) || "非同组织".equals(fjsfs)){
                glf++;
            }
        }

        //开始赋值

        billIssueUtil.setBillNumber(wfzGjjft);//单据编号
        wfzGjjft.setFcompany(billIssueUtil.costNumber(fcompany));//组织
        wfzGjjft.setFperiod(fperiod);//薪酬期间
        wfzGjjft.setFzrs(zrs);//总人数
        wfzGjjft.setFnscbzxrs(BigDecimal.valueOf(nscbzxrs));//合并纳税成本中心人数
        wfzGjjft.setFglf(BigDecimal.valueOf(glf));//关联方人数
        wfzGjjft.setFcreator(username);//创建者
        wfzGjjft.setFlastupdateuser(username);//最后修改者
        wfzGjjft.setFstate("SAVE");//状态
        wfzGjjft.setFsourcebillid(String.valueOf(billId));//来源单ID
        wfzGjjft.setFiseas(0L);//是否同步至eas

        wfzGjjft.setWfzGjjftentryList(wfzGjjftentries);//设置单据分录

        int i = wfzGjjftService.insertWfzGjjft(wfzGjjft);
        return i;
    }
}
